package com.feicheng.controller.main.admin.client;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Account;
import com.feicheng.persistence.entity.ClientDeposit;
import com.feicheng.persistence.model.ClientDepositPlus;
import com.feicheng.persistence.model.CutDepositRecordMain;
import com.feicheng.persistence.model.DepositRecordMain;
import com.feicheng.persistence.service.IAccountService;
import com.feicheng.persistence.service.IClientDepositService;
import com.feicheng.persistence.service.IDepositRecordService;
import com.feicheng.service.IFClientService;
import com.feicheng.service.IFHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/main/admin/client/deposit-record")
@RequestMapping("/main/admin/client/deposit-record")
public class DepositRecordController {

    @Autowired
    IFClientService fClientService;

    @Autowired
    IFHomeService fHomeService;
    
    @Autowired
    IDepositRecordService depositRecordService;

    @Autowired
    IClientDepositService clientDepositService;

    @Autowired
    IAccountService accountService;
    
    /**
     * 分页查询押金记录V2
     * @return
     */
    @GetMapping("/list/v2")
    public ResultInfo getListV2(
    		ClientDepositPlus clientDepositPlus,
            @RequestParam(defaultValue="1") Integer current,
            @RequestParam(defaultValue="10")Integer size
    ){
    	
    	QueryWrapper<ClientDepositPlus> clientDepositPlusWrapper = new QueryWrapper<>();
    	
    	if (null != clientDepositPlus.getStoreId()) {
    		clientDepositPlusWrapper.eq("store_id", clientDepositPlus.getStoreId());
    	}
    	
    	if (null != clientDepositPlus.getClientName()) {
    		clientDepositPlusWrapper.like("client_name", clientDepositPlus.getClientName());
    	}

        if (null != clientDepositPlus.getOriginalCode() && clientDepositPlus.getOriginalCode() != ""){
            clientDepositPlusWrapper.like("original_code",clientDepositPlus.getOriginalCode());
        }
    	
    	if (null != clientDepositPlus.getStatu() && clientDepositPlus.getStatu() != "") {
    		clientDepositPlusWrapper.like("statu", clientDepositPlus.getStatu());
    	}
    	
    	clientDepositPlusWrapper.isNotNull(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE);
    	
    	clientDepositPlusWrapper.eq(Constant.ENTITY.DEPOSIT_RECORD.TYPE, Constant.VAL.DEPOSIT.INS);

    	clientDepositPlusWrapper.orderByDesc("statu", "ct");
    	
    	Page<ClientDepositPlus> page = new Page<ClientDepositPlus>(current, size);
    	
        return ResultInfo.success(depositRecordService.getClientDepositPlusByPage(page, clientDepositPlusWrapper));
    }
    /**
     * 修改押金状态V2
     * @return
     */
    @GetMapping("/update/v2")
    public ResultInfo getListV2(
            ClientDeposit clientDeposit
    ){
        QueryWrapper<ClientDeposit> clientDepositQueryWrapper = new QueryWrapper<>();
        clientDepositQueryWrapper.eq(Constant.ENTITY.CLIENT_DEPOSIT.DEPOSIT_ID,clientDeposit.getDepositId());
        clientDepositService.update(clientDeposit,clientDepositQueryWrapper);
        if (clientDeposit.getStatu().equals("BCK")){
            Account account = new Account();
            account.setDepositAmount(0);
            QueryWrapper<Account> queryWrapper = new QueryWrapper();
            queryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID,clientDeposit.getClientId());
            accountService.update(account,queryWrapper);
        }
        return ResultInfo.success();
    }


    /**
     * 扣押押金记录V2
     * @return
     */
    @GetMapping("/cut/v2")
    public ResultInfo getListV2(
            Integer depositId,
            Integer clientId
    ){
        ClientDeposit clientDeposit = new ClientDeposit();
        clientDeposit.setDepositId(depositId);
        clientDeposit.setStatu("APP");
        QueryWrapper<ClientDeposit> clientDepositQueryWrapper = new QueryWrapper<>();
        clientDepositQueryWrapper.eq(Constant.ENTITY.CLIENT_DEPOSIT.DEPOSIT_ID,depositId);
        clientDepositService.update(clientDeposit,clientDepositQueryWrapper);
        Account account = new Account();
        account.setDepositAmount(0);
        QueryWrapper<Account> queryWrapper = new QueryWrapper();
        queryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID,clientId);
        accountService.update(account,queryWrapper);
        return ResultInfo.success();
    }


    /**
     * 分页查询押金记录
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultInfo getList(
            DepositRecordMain depositRecordMain,
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10")Integer size
    ){
        return fClientService.getDepositRecordByPage(depositRecordMain, currentPage, size);
    }

    @GetMapping("/details")//取得押金记录
    public ResultInfo getDepositRecordList(Integer depositId){
        return fClientService.getDepositRecord(depositId);
    }
    
    @PostMapping("/newDeposit")//新增押金记录
    public ResultInfo newDepositRecord(
            Integer amount,
            Integer clientId,
            Integer storeId
    ){
        JSONObject result = new JSONObject();
        result.put(Constant.ENTITY.DEPOSIT_RECORD.AMOUNT,amount);
        result.put(Constant.ENTITY.DEPOSIT_RECORD.TYPE,"INS");
        result.put(Constant.ENTITY.DEPOSIT_RECORD.REASON,"押金充值");
        result.put(Constant.ENTITY.CLIENT.CLIENT_ID,clientId);
        result.put(Constant.ENTITY.STORE.STORE_ID,storeId);
        return fClientService.newDepositRecord(result);

    }

    //总店分页查询扣押记录列表
    @GetMapping("/cut/list")
    public ResultInfo getcutDepositRecord(
            String clientName,
            String ct,
            String lastct,
            String name,
            String ioFlg,
            String account,
            String storeId,
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10")Integer size
    ){
        CutDepositRecordMain cutDepositRecordMain = new CutDepositRecordMain();
        if (null != ct){
            LocalDateTime ctTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(ct)), ZoneId.systemDefault());
            cutDepositRecordMain.setCt(ctTime);
        }
        if (null != lastct){
            LocalDateTime lastctTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(lastct)), ZoneId.systemDefault());
            cutDepositRecordMain.setLastct(lastctTime);
        }
        JSONObject result = new JSONObject();
        result.put(Constant.ENTITY.STORE.STORE_ID,storeId);
        result.put(Constant.ENTITY.CLIENT.CLIENT_NAME,clientName);
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.CT,cutDepositRecordMain.getCt());
        result.put(Constant.HTTP.PARAM.LASTCT,cutDepositRecordMain.getLastct());
        result.put(Constant.ENTITY.STORE.NAME,name);
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG,ioFlg);
        result.put(Constant.ENTITY.USER.ACCOUNT,account);
        result.put(Constant.HTTP.PARAM.CURRENT,currentPage);
        result.put(Constant.HTTP.PARAM.SIZE,size);
        return  fClientService.getCutDepositRecordByPage(result);
    }

    /**
     * 取得扣押记录详情
     * @param cutId
     * @return
     */
    @GetMapping("/{cutId}/cut")
    public ResultInfo getCutDepositRecord(@PathVariable(name="cutId") Integer cutId) {
        return fClientService.getCutDepositRecord(cutId);
    }

    @PutMapping("/{cutId}/cut/cancel")
    public ResultInfo cancelCutDeposit(@PathVariable(name="cutId") Integer cutId) {
        return fClientService.cancelCutDeposit(cutId);
    }

    /**
     * 新增扣押记录
     * @param amount
     * @param reason
     * @param clientId
     * @return
     */
    @PostMapping("/{clientId}/cut")
    public ResultInfo newCutDepositRecord(
            Integer amount,
            String reason,
            @PathVariable(name="clientId") Integer clientId
    ){
        return ResultInfo.success(fClientService.cutDeposit(amount, reason, clientId));
    }
}
